//
//  HJCodeView.m
//  Sneakers
//
//  Created by 胡光健 on 2017/10/21.
//  Copyright © 2017年 mac. All rights reserved.
//

#import "HJCodeView.h"
#import "HJHomeModel.h"
#import "HJDetailCollectionCell.h"

@interface HJCodeView ()<UICollectionViewDelegate,UICollectionViewDataSource>
@property(nonatomic,strong) UICollectionView * collectionView;
@property(nonatomic,strong) NSMutableArray *sizeArr;
@property(nonatomic,strong) UIButton *btn;

@end

@implementation HJCodeView
-(NSMutableArray *)sizeArr {
    if (!_sizeArr) {
        _sizeArr = [NSMutableArray array];
    }
    return _sizeArr;
}

-(instancetype)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        self.frame = frame;
        [self configCollection];
    }
    return self;
}
-(void)awakeFromNib {
    [super awakeFromNib];
    
}
-(void)configCollection {
    
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc]init];
    [layout setScrollDirection:UICollectionViewScrollDirectionVertical];
    layout.itemSize = CGSizeMake((mScreenWidth -18)/5, (mScreenWidth -18)/5);
    layout.headerReferenceSize = CGSizeMake(mScreenWidth, 50);
    
    _collectionView = [[UICollectionView alloc]initWithFrame:self.bounds collectionViewLayout:layout];
    _collectionView.showsHorizontalScrollIndicator = NO;
    _collectionView.backgroundColor = HJColorRGB(10);
    _collectionView.bounces = NO;
    _collectionView.backgroundColor = [UIColor whiteColor];
    _collectionView.delegate = self;
    _collectionView.dataSource = self;
    //注册头视图
    [self.collectionView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"Header"];
    [_collectionView registerClass:[HJDetailCollectionCell class] forCellWithReuseIdentifier:@"Cell"];
    [self addSubview:self.collectionView];
    
}
-(void)layoutSubviews {
    [super layoutSubviews];
    _collectionView.frame = CGRectMake(0, 0, self.width, self.height);
}
-(void)loadSizeWorkWithShoesid:(NSNumber *)shoes_id size:(NSString *)size {
    NSMutableDictionary  * params = [NSMutableDictionary dictionary];
    params[@"shoes_id"] = shoes_id;
    params[@"size"] = size;
    [TZHttpTool post:ShoesSizeAPI params:params success:^(NSDictionary *result) {
        NSArray * sizeS = [HJHomeDetailCodeModel mj_objectArrayWithKeyValuesArray:result[@"data"]];
        [self.sizeArr removeAllObjects];
        [self.sizeArr addObjectsFromArray:sizeS];
        for (int i =0; i<sizeS.count; i++) {
            HJHomeDetailCodeModel * model = sizeS[i];
            if ([model.check isEqual:@1]) {
                self.currentCheckModel = model;
                break;
            }else {
                continue;
            }
        }
        [self.collectionView reloadData];
    } failure:^(NSString *msg) {
    }];
}

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    return 1;
}

//每个section的item个数
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return self.sizeArr.count;
}

-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    HJDetailCollectionCell * cell = (HJDetailCollectionCell *)[collectionView dequeueReusableCellWithReuseIdentifier:@"Cell" forIndexPath:indexPath];
    HJHomeDetailCodeModel * model = self.sizeArr[indexPath.row];
    if ([model.often_size isEqual:@1]) {
        cell.hideLabel.hidden = NO;
    }else {
        cell.hideLabel.hidden = YES;
    }
    
    cell.codel.text = model.size;
    cell.money.text = model.cost;
    if ([model.check isEqual:@0]) {
        cell.bgView.backgroundColor = HJColorRGB(250);
        cell.codel.textColor = HJColorRGB(135);
        cell.money.textColor = HJColorRGB(135);
        cell.hideLabel.textColor = HJColorRGB(135);
    }else {
        cell.bgView.backgroundColor = HJColorRGB(127);
        cell.codel.textColor = HJColorRGB(255);
        cell.money.textColor = HJColorRGB(255);
        cell.hideLabel.textColor = HJColorRGB(255);
    }
    return cell;
}

//创建头视图
- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath {
    if (kind == UICollectionElementKindSectionHeader) {
        UICollectionReusableView *headView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"Header" forIndexPath:indexPath];
        self.btn = [UIButton buttonWithType:UIButtonTypeCustom];
        [self.btn setImage:HJImage(@"close") forState:UIControlStateNormal];
        self.btn.frame = CGRectMake(headView.width-15-40, headView.centerY-20, 40, 40);
        [self.btn addTarget:self action:@selector(closeBtn) forControlEvents:UIControlEventTouchUpInside];
        [headView addSubview:self.btn];
        
        UILabel * label = [self configWithLabel:@"尺码选择" textColor:HJColorRGB(0) textAlignment:NSTextAlignmentLeft frame:CGRectMake(15, headView.centerY-20, [CommonTools sizeWithText:@"尺码选择" font:17].width + 10, 40) font:HJFontSize(17)];
        [headView addSubview:label];
        
        return headView;
    }
    return nil;
}

- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section {
    
    return UIEdgeInsetsMake(1.5, 1.5, 0.75, 1.5);
}

//设置每个item水平间距
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section {
    return 0.001;
}

//点击选定
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    if ([self.selecte_cost isEqual:@1]) {
        HJHomeDetailCodeModel * model = self.sizeArr[indexPath.row];
        if (![model.cost isEqualToString:@""]) {
            for (HJHomeDetailCodeModel * model in self.sizeArr) {
                if ([model.check isEqual:@1]) {
                    model.check = @0;
                }
            }
            model.check = @1;
        }
    }else {
        HJHomeDetailCodeModel * model = self.sizeArr[indexPath.row];
        if (self.currentCheckModel) {
            _currentCheckModel.check = @0;
        }
        model.check = @1;
        _currentCheckModel = model;
    }
    [collectionView reloadData];
}


/***关闭弹框**/
-(void)closeBtn {
    NSNumber * staute;
    if (self.type==cost_is_nil) {
        staute = @0;
    }else {
        staute = @1;
    }
    for (HJHomeDetailCodeModel * model in self.sizeArr) {
        if ([model.check isEqual:@1]) {
            if ([model.cost isEqualToString:@""]) {
                self.cost = @"0.00";
            }else {
                self.cost = model.cost;
            }
            self.size = [NSString stringWithFormat:@"%@",model.size];
            self.shoes_id = model.shoes_id;
        }
    }
    if ([self.delegate respondsToSelector:@selector(CodeViewDidClickOKButton:shoes_id:cost:type:)]) {
        [self.delegate CodeViewDidClickOKButton:self.size shoes_id:self.shoes_id cost:self.cost type:staute];
    }
}

@end
